<template>
  <div class="register">
    <el-container>
      <el-card class="form-card">
        <el-form :model="registerForm" ref="registerForm" :rules="rules">
          <h3>登录</h3>
          <el-form-item prop="username">
            <el-input placeholder="请输入用户名" v-model="registerForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" v-model="registerForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item prop="code" class="identifyCode">
            <el-input placeholder="请输入验证码" v-model="registerForm.code"></el-input>
            <div @click="refreshCode">
              <s-identify :identifyCode="identifyCode"></s-identify>
            </div>
          </el-form-item>
          <el-form-item size="mini" style="margin: -5px 0">
            <el-link :underline="false" @click="$router.push('/forget')" type="primary" style="float: right">
              忘记密码
            </el-link>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('registerForm')">登录</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="register">注册</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-container>

  </div>
</template>

<script>
import SIdentify from "@/components/SIdentify";
import {Post} from "@/plugins/axios";

export default {
  components: {
    SIdentify
  },
  data() {
    return {
      identifyCode: '验 证 码 ',
      identifyCodes: '42c2huHjFiW9D9DdJ99DjJD834sakju88HG89',
      registerForm: {
        username: '',
        password: '',
        code: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 4, max: 4, message: '长度为 4 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  mounted() {
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //验证码认证
          if (this.registerForm.code === this.identifyCode) {
            const _this = this
            Post('/user/login', this.registerForm)
                .then(function (res) {
                  if (res.data.code === 200) {
                    _this.$message({
                      showClose: true,
                      message: res.data.message,
                      type: 'success',
                      center: true
                    });
                    sessionStorage.setItem('token', res.headers.token)

                    console.log("data", res.data)
                    switch (res.data.data.role) {
                      case 'student':
                        _this.$router.push('/student/courseInfo');
                        break;
                      case 'teacher':
                        _this.$router.push('/teacher/courseInfo');
                        break;
                      case 'admin':
                        _this.$router.push('/admin/userInfo');
                        break;
                    }

                  } else {
                    _this.$confirm(res.data.message, '提示', {
                      confirmButtonText: '确定',
                      cancelButtonText: '取消',
                      type: 'warning',
                      center: true
                    })
                  }
                })
          } else {
            this.$message({
              showClose: true,
              message: "验证码错误",
              type: 'error',
              center: true
            })
            return false;
          }
        } else {
          this.$message({
            showClose: true,
            message: "提交错误",
            type: 'error',
            center: true
          });
          return false;
        }
      });
    },
    register() {
      this.$router.push('/register')
    },

    //验证码相关
    refreshCode() {
      this.identifyCode = ""
      this.makeCode(this.identifyCodes.split(''), 4)
    },
    //产生[min, max]范围的随机数
    randomNum(min, max) {
      max = max + 1
      return Math.floor(min + Math.random() * (max - min))
    },
    makeCode(data, len) {
      for (let i = 0; i < len; i++) {
        this.identifyCode += data[this.randomNum(0, data.length - 1)]
      }
    }
  }
}
</script>

<style lang="scss" scoped>

.el-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login-bg.jpg") no-repeat center/cover;
}

h3 {
  margin: 16px;
}

.form-card {
  width: 400px;
  background: #e0c8d1;
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .5);
}

.identifyCode {
  .el-input {
    width: 200px;
    float: left;
  }

  div {
    float: right;
  }
}

.el-button {
  width: 100%;
}
</style>